package pv.finder;

import java.util.ArrayList;
import java.util.List;

import pv.finder.FMOption.MyListAdapter;
import pv.finder.MySetting.MAPMODE;
import pv.finder.MySetting.ROUTEMODE;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ExpandableListView;

/**
 * Created by hitle on 6/12/13.
 */
public class FMSetting extends FMOption implements
		ExpandableListView.OnChildClickListener,
		ExpandableListView.OnGroupClickListener {

	protected MyListAdapter initData() {
		// TODO Auto-generated method stub
		MyListAdapter adapter = new MyListAdapter(getActivity());
		List<ItemMenu> listData = new ArrayList<ItemMenu>();
		MySetting mySetting = MySetting.getInstance();

		String[] settins = getResources().getStringArray(R.array.setting);
		// maptype
		ItemMenu mapType = new ItemMenu(0, settins[0], R.drawable.icon_map);

		mapType.setListChild(new ChildItemMenu[] {
		/* normap */
		new ChildItemMenu(1, settins[1]),
		/* Hybrid */
		new ChildItemMenu(2, settins[2]),
		/* Satellite */
		new ChildItemMenu(3, settins[3]),
		/* Terrain */
		new ChildItemMenu(4, settins[4]) });

		for (int i = 0; i < mapType.getListChild().length; i++) {

			if (i == mySetting.getMap().ordinal()) {

				mapType.setChoosedId(mapType.getListChild()[i].getId());
			}

		}

		listData.add(mapType);

		// Route Travel Modes
		ItemMenu routeType = new ItemMenu(5, settins[5],
				R.drawable.icon_direction);
		routeType.setListChild(new ChildItemMenu[] {
		/* normap */
		new ChildItemMenu(6, settins[6]),
		/* Hybrid */
		new ChildItemMenu(7, settins[7]),
		/* Satellite */
		new ChildItemMenu(8, settins[8]),
		/* Terrain */
		new ChildItemMenu(9, settins[9]) });
		for (int i = 0; i < routeType.getListChild().length; i++) {

			if (i == mySetting.getRoute().ordinal()) {

				routeType.setChoosedId(routeType.getListChild()[i].getId());
			}

		}
		listData.add(routeType);

		// full screen
		ItemMenu fullscreen = new ItemMenu(10, settins[10],
				R.drawable.icon_full_scren);

		listData.add(fullscreen);

		adapter.setListData(listData);

		return adapter;

	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		Fragment f = getActivity().getSupportFragmentManager()
				.findFragmentById(R.id.main_content);
		if (f instanceof FMMap) {
			// map type normal

			MySetting settings = MySetting.getInstance();
			MAPMODE mapMode = MAPMODE.NORMAL;

			if (groupPosition == 0) {
				if (childPosition == 0) {

					mapMode = MAPMODE.NORMAL;
					settings.setMap(MAPMODE.NORMAL);

				}/* map type hybrid */else if (childPosition == 1) {
					mapMode = MAPMODE.HYBRID;
					settings.setMap(MAPMODE.HYBRID);

				}/* map type satellite */else if (childPosition == 2) {
					mapMode = MAPMODE.SATELLITE;
					settings.setMap(MAPMODE.SATELLITE);

				} /* map type terrain */else if (childPosition == 3) {
					mapMode = MAPMODE.TERRAIN;
					settings.setMap(MAPMODE.TERRAIN);
				}
			} else if (groupPosition == 1) {

				/* driving */if (childPosition == 0) {
					settings.setRoute(ROUTEMODE.DRIVING);

				}/* walking */else if (childPosition == 1) {
					settings.setRoute(ROUTEMODE.WARLKING);

				}/* bicycling */else if (childPosition == 2) {
					settings.setRoute(ROUTEMODE.BICYLING);

				} /* transit */else if (childPosition == 3) {
					settings.setRoute(ROUTEMODE.TRANSIT);

				}
			}
			settings.saveSetting(getActivity());
			MyListAdapter mla = ((MyListAdapter) parent
					.getExpandableListAdapter());

			((ItemMenu) mla.getGroup(groupPosition))
					.setChoosedId(((ChildItemMenu) mla.getChild(groupPosition,
							childPosition)).getId());

			((MyListAdapter) parent.getExpandableListAdapter())
					.notifyDataSetChanged();

			if (groupPosition == 0) {
				((FMMap) f).showMapType(mapMode);
			}
			// ((FMMap) f).removeSubContent();
		}
		return false;
	}

	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		// TODO Auto-generated method stub
		Fragment f = getActivity().getSupportFragmentManager()
				.findFragmentById(R.id.main_content);
		if (f instanceof FMMap) {
			if (groupPosition == 2) {

				((FMMap) f).fullScreen();

				((FMMap) f).removeSubContent();
			}
			// ((FMMap) f).removeSubContent();
		}
		return true;

	}

}
